package com.wing.cabinet.controller.app;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wing.cabinet.model.entity.Cabinet;
import com.wing.cabinet.model.entity.CabinetLoadPlan;
import com.wing.cabinet.model.request.CabinetLoadPlanForm;
import com.wing.cabinet.model.response.CabinetCargoLaneVO;
import com.wing.cabinet.model.response.CabinetLoadPlanDetailVO;
import com.wing.cabinet.model.response.CabinetLoadPlanVO;
import com.wing.cabinet.model.response.CabinetVO;
import com.wing.cabinet.service.CabinetCargoLaneService;
import com.wing.cabinet.service.CabinetLoadPlanDetailService;
import com.wing.cabinet.service.CabinetLoadPlanService;
import com.wing.cabinet.service.CabinetService;
import com.wing.common.exception.BusinessException;
import com.wing.common.utils.JsonResult;
import com.wing.common.utils.PageResult;
import com.wing.enterprise.product.model.response.ProductPackLabelVO;
import com.wing.order.model.entity.DistributionDemand;
import com.wing.order.model.response.DistributionDemandDetailVO;
import com.wing.order.model.response.DistributionDemandVO;
import com.wing.order.model.response.InventoryAlarmDetailVO;
import com.wing.order.model.response.InventoryAlarmVO;
import com.wing.order.service.DistributionDemandDetailService;
import com.wing.order.service.DistributionDemandService;
import com.wing.order.service.InventoryAlarmDetailService;
import com.wing.order.service.InventoryAlarmService;
import com.wing.product.model.response.ProductDetailVO;
import com.wing.product.service.ProductDetailService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 *
 * @author
 */
@Validated
@Api(value = "App-上货计划管理", tags = "上货计划管理")
@RestController("appCabinetLoadPlanController")
@RequestMapping("cabinet/app/cabinetLoadPlan")
@Slf4j
public class CabinetLoadPlanController {

    @Autowired
    private CabinetLoadPlanService cabinetLoadPlanService;
    @Autowired
    private DistributionDemandService distributionDemandService;
    @Autowired
    private DistributionDemandDetailService distributionDemandDetailService;
    @Autowired
    private InventoryAlarmService inventoryAlarmService;
    @Autowired
    private InventoryAlarmDetailService inventoryAlarmDetailService;
    @Autowired
    private CabinetService cabinetService;
    @Autowired
    private CabinetCargoLaneService cabinetCargoLaneService;
    @Autowired
    private ProductDetailService productDetailService;
    @Autowired
    private CabinetLoadPlanDetailService cabinetLoadPlanDetailService;

    @ApiOperation(value = "查询所有上货计划", notes = "搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "第几页", defaultValue = "1", example = "1", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "limit", value = "每页多少条", defaultValue = "10", example = "10", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "sort", value="排序字段名", defaultValue = "create_time", example = "create_time", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "order", value="排序规则(asc:正序, desc:倒序)", defaultValue = "asc", example = "asc", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "code", value = "编号", defaultValue = "", example = "", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "distributionDemandId", value = "配货需求单id", defaultValue = "", example = "", dataType = "Long", paramType = "query")
    })
    @GetMapping("/list")
    public PageResult<CabinetLoadPlanVO> list(
            @RequestParam(defaultValue = "1") Integer page
            ,@RequestParam(defaultValue = "10") Integer limit
            ,@RequestParam(defaultValue = "create_time") String sort
            ,@RequestParam(defaultValue = "desc") String order
            ,String code
            ,Long distributionDemandId
            ,Long id
    ){
        Page<CabinetLoadPlan> pg = new Page<>(page, limit);
        Map<String, Object> params = new HashMap<>();

        //排序
        if (StringUtils.isNotBlank(sort) && StringUtils.isNotBlank(order)) {
            params.put("sort", sort);
            params.put("order", order);
        }
        //搜索
        if (StringUtils.isNotBlank(code)) {
            params.put("code", code);
        }
        params.put("distributionDemandId",distributionDemandId);
        params.put("id",id);
        Page<CabinetLoadPlanVO> list = cabinetLoadPlanService.pageList4app(pg, params);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }

    @ApiOperation(value = "添加上货计划")
    @PostMapping("/add")
    public JsonResult add(@Validated(CabinetLoadPlanForm.Add.class) @RequestBody CabinetLoadPlanForm cabinetLoadPlanForm) {
        try {
            if (cabinetLoadPlanService.add(cabinetLoadPlanForm) > 0) {
                return JsonResult.success("添加成功");
            }
            return JsonResult.fail("添加失败");
        } catch (BusinessException e) {
            return JsonResult.fail(e.getCode(), e.getMessage());
        } catch (Exception e) {
            return JsonResult.fail(e.getMessage());
        }
    }

    @ApiOperation(value = "查询所有配货需求订单表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "第几页", defaultValue = "1", example = "1", dataType = "Integer", paramType = "query")
            , @ApiImplicitParam(name = "limit", value = "每页多少条", defaultValue = "10", example = "10", dataType = "Integer", paramType = "query")
            , @ApiImplicitParam(name = "sort", value = "排序字段名", defaultValue = "create_time", example = "create_time", dataType = "String", paramType = "query")
            , @ApiImplicitParam(name = "order", value = "排序规则(asc:正序, desc:倒序)", defaultValue = "asc", example = "asc", dataType = "String", paramType = "query")
            , @ApiImplicitParam(name = "distCode", value = "配货订单编号", defaultValue = "", example = "", dataType = "String", paramType = "query")
    })
    @GetMapping("/distributionDemandList")
    public PageResult<DistributionDemandVO> distributionDemandList(
            @RequestParam(defaultValue = "1") Integer page
            , @RequestParam(defaultValue = "10") Integer limit
            , @RequestParam(defaultValue = "create_time") String sort
            , @RequestParam(defaultValue = "desc") String order
            , String distCode
    ) {
        Page<DistributionDemand> pg = new Page<>(page, limit);
        Map<String, Object> params = new HashMap<>();

        //排序
        if (StringUtils.isNotBlank(sort) && StringUtils.isNotBlank(order)) {
            params.put("sort", sort);
            params.put("order", order);
        }
        if (StringUtils.isNotBlank(distCode)) {
            params.put("distCode", distCode);
        }
        //搜索
        Page<DistributionDemandVO> list = distributionDemandService.pageList4app(pg, params);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }

    @ApiOperation(value = "查询所有配货需求计划明细表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "第几页", defaultValue = "1", example = "1", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "limit", value = "每页多少条", defaultValue = "10", example = "10", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "sort", value="排序字段名", defaultValue = "create_time", example = "create_time", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "order", value="排序规则(asc:正序, desc:倒序)", defaultValue = "asc", example = "asc", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "distCode", value="配货订单编号", defaultValue = "", example = "", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "warehouseId", value="仓库ID", defaultValue = "", example = "", dataType = "Long", paramType = "query")

    })
    @GetMapping("/distributionDemandDetailList")
    public PageResult<DistributionDemandDetailVO> distributionDemandDetailList(
            @RequestParam(defaultValue = "1") Integer page
            ,@RequestParam(defaultValue = "10") Integer limit
            ,@RequestParam(defaultValue = "create_time") String sort
            ,@RequestParam(defaultValue = "asc") String order
            ,String distCode
            ,Long warehouseId
    ){

        Page<DistributionDemandDetailVO> pg = new Page<>(page, limit);
        Map<String, Object> params = new HashMap<>();

        //排序
        if (StringUtils.isNotBlank(sort) && StringUtils.isNotBlank(order)) {
            params.put("sort", sort);
            params.put("order", order);
        }

        //搜索
        if(StringUtils.isNotBlank(distCode)){
            params.put("distCode", distCode);
        }
        if(warehouseId != null){
            params.put("warehouseId", warehouseId);
        }

        IPage<DistributionDemandDetailVO> list = distributionDemandDetailService.pageList4app(pg,params);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }

    @ApiOperation(value = "查询所有刀柜")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "第几页", defaultValue = "1", example = "1", dataType = "Integer", paramType = "query")
            , @ApiImplicitParam(name = "limit", value = "每页多少条", defaultValue = "10", example = "10", dataType = "Integer", paramType = "query")
            , @ApiImplicitParam(name = "sort", value = "排序字段名", defaultValue = "create_time", example = "create_time", dataType = "String", paramType = "query")
            , @ApiImplicitParam(name = "order", value = "排序规则(asc:正序, desc:倒序)", defaultValue = "asc", example = "asc", dataType = "String", paramType = "query")
            , @ApiImplicitParam(name = "cabinetTypeId", value = "刀柜类型ID", defaultValue = "cabinetTypeId", example = "cabinetTypeId", dataType = "String", paramType = "query")
            , @ApiImplicitParam(name = "cabinetBrandId", value = "刀柜品牌id", defaultValue = "cabinetBrandId", example = "cabinetBrandId", dataType = "String", paramType = "query")
            , @ApiImplicitParam(name = "status", value = "状态", defaultValue = "status", example = "status", dataType = "Integer", paramType = "query")
            , @ApiImplicitParam(name = "code", value = "刀柜编码", defaultValue = "code", example = "code", dataType = "String", paramType = "query")
            , @ApiImplicitParam(name = "cabinetNature", value = "刀柜性质", defaultValue = "sale", example = "sale", dataType = "String", paramType = "query")
    })
    @GetMapping("/cabinetList")
    public PageResult<CabinetVO> cabinetList(
            @RequestParam(defaultValue = "1") Integer page
            , @RequestParam(defaultValue = "10") Integer limit
            , @RequestParam(defaultValue = "create_time") String sort
            , @RequestParam(defaultValue = "asc") String order
            , String cabinetTypeId
            , String cabinetBrandId
            , Cabinet.Status status
            , String code
            , @RequestParam(defaultValue = "sale") Cabinet.CabinetNature cabinetNature
    ) {
        Page<CabinetVO> pg = new Page<>(page, limit);
        Map<String, Object> params = new HashMap<>();

        //排序
        if (StringUtils.isNotBlank(sort) && StringUtils.isNotBlank(order)) {
            params.put("sort", sort);
            params.put("order", order);
        }
        //搜索
        if (StringUtils.isNotBlank(cabinetTypeId)) {
            params.put("cabinetTypeId", cabinetTypeId);

        }
        if (StringUtils.isNotBlank(cabinetBrandId)) {
            params.put("cabinetBrandId", cabinetBrandId);

        }
        if (Objects.nonNull(status)) {
            params.put("status", status.getValue());

        }
        if (StringUtils.isNotBlank(code)) {
            params.put("code", code);

        }
        if (cabinetNature != null) {
            params.put("cabinetNature", cabinetNature);
        }
        Page<CabinetVO> list = cabinetService.pageList4app(pg, params);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }

    @ApiOperation(value = "查询所有刀柜货道")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "第几页", defaultValue = "1", example = "1", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "limit", value = "每页多少条", defaultValue = "10", example = "10", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "sort", value="排序字段名", defaultValue = "create_time", example = "create_time", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "order", value="排序规则(asc:正序, desc:倒序)", defaultValue = "asc", example = "asc", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "code", value = "编号", defaultValue = "", example = "", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "cabCode", value = "刀柜编号", defaultValue = "", example = "", dataType = "String", paramType = "query")

    })
    @GetMapping("/cabinetCargoLaneList")
    public PageResult<CabinetCargoLaneVO> cabinetCargoLaneList(
            @RequestParam(defaultValue = "1") Integer page
            ,@RequestParam(defaultValue = "10") Integer limit
            ,@RequestParam(defaultValue = "create_time") String sort
            ,@RequestParam(defaultValue = "desc") String order
            ,String code
            ,String cabCode
            ,Integer isFilterCargoLane
            ,Long warehouseId
    ){

        Page<CabinetCargoLaneVO> pg = new Page<>(page, limit);
        Map<String, Object> params = new HashMap<>();

        //排序
        if (StringUtils.isNotBlank(sort) && StringUtils.isNotBlank(order)) {
            params.put("sort", sort);
            params.put("order", order);
        }

        //搜索
        if (StringUtils.isNotBlank(code)) {
            params.put("code", code);
        }
        if(StringUtils.isNotBlank(cabCode)){
            params.put("cabCode",cabCode);
        }
        if(isFilterCargoLane != null){
            params.put("isFilterCargoLane", isFilterCargoLane);
        }
        if(warehouseId != null){
            params.put("warehouseId",warehouseId);
        }
        Page<CabinetCargoLaneVO> list = cabinetCargoLaneService.pageList4app(pg, params);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }

    /*@ApiOperation(value = "查询所有仓库")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "第几页", defaultValue = "1", example = "1", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "limit", value = "每页多少条", defaultValue = "10", example = "10", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "sort", value="排序字段名", defaultValue = "create_time", example = "create_time", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "order", value="排序规则(asc:正序, desc:倒序)", defaultValue = "asc", example = "asc", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "code", value = "编码", defaultValue = "", example = "", dataType = "String", paramType = "query")
    })
    @GetMapping("/warehouseList")
    public PageResult<WarehouseVO> warehouseList(
            @RequestParam(defaultValue = "1") Integer page
            ,@RequestParam(defaultValue = "10") Integer limit
            ,@RequestParam(defaultValue = "create_time") String sort
            ,@RequestParam(defaultValue = "asc") String order
            ,String name
            ,String code
    ){
        Page pg=new Page(page,limit);
        Map queryParam=new HashMap();
        queryParam.put("sort", StrUtil.toUnderlineCase(sort));
        queryParam.put("order",order);
        if(StringUtils.isNotBlank(name)){
            queryParam.put("name",name);
        }
        if(StringUtils.isNotBlank(code)){
            queryParam.put("code",code);
        }
        IPage<WarehouseVO> list = warehouseService.pageList4app(pg,queryParam);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }*/

    @ApiOperation(value = "查询所有产品明细表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "第几页", defaultValue = "1", example = "1", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "limit", value = "每页多少条", defaultValue = "10", example = "10", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "sort", value="排序字段名", defaultValue = "create_time", example = "create_time", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "order", value="排序规则(asc:正序, desc:倒序)", defaultValue = "asc", example = "asc", dataType = "String", paramType = "query")
    })
    @GetMapping("/productDetailList")
    public PageResult<ProductDetailVO> productDetailList(
            @RequestParam(defaultValue = "1") Integer page
            ,@RequestParam(defaultValue = "10") Integer limit
            ,@RequestParam(defaultValue = "create_time") String sort
            ,@RequestParam(defaultValue = "asc") String order
            ,Long warehouseId
    ){
        Page pg=new Page(page,limit);
        Map queryParam=new HashMap();
        queryParam.put("sort",StrUtil.toUnderlineCase(sort));
        queryParam.put("order",order);
        //搜索
        if(warehouseId != null){
            queryParam.put("warehouseId", warehouseId);
        }
        Page<ProductDetailVO> list = productDetailService.pageList4app(pg, queryParam);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }

    @ApiOperation(value = "查询所有上货计划明细")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "第几页", defaultValue = "1", example = "1", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "limit", value = "每页多少条", defaultValue = "10", example = "10", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "sort", value="排序字段名", defaultValue = "create_time", example = "create_time", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "order", value="排序规则(asc:正序, desc:倒序)", defaultValue = "asc", example = "asc", dataType = "String", paramType = "query")
    })
    @GetMapping("/cabinetLoadPlanDetailList")
    public PageResult<CabinetLoadPlanDetailVO> cabinetLoadPlanDetailList(
            @RequestParam(defaultValue = "1") Integer page
            ,@RequestParam(defaultValue = "10") Integer limit
            ,@RequestParam(defaultValue = "create_time") String sort
            ,@RequestParam(defaultValue = "asc") String order
            ,Long cabinetLoadPlanId
            ,String type
    ){
        Page pg = new Page(page,limit);
        Map queryParam=new HashMap();
        queryParam.put("sort",StrUtil.toUnderlineCase(sort));
        queryParam.put("order",order);
        //搜索
        if(cabinetLoadPlanId != null){
            queryParam.put("cabinetLoadPlanId", cabinetLoadPlanId);
        }
        if(StringUtils.isNotBlank(type)){
            queryParam.put("type", type);
        }
        Page<CabinetLoadPlanDetailVO> list = cabinetLoadPlanDetailService.pageList4app(pg, queryParam);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }

    @ApiOperation(value = "查询所有库存告警")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "第几页", defaultValue = "1", example = "1", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "limit", value = "每页多少条", defaultValue = "10", example = "10", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "sort", value="排序字段名", defaultValue = "create_time", example = "create_time", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "order", value="排序规则(asc:正序, desc:倒序)", defaultValue = "asc", example = "asc", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "demandId", value="配货订单ID", defaultValue = "", example = "", dataType = "Long", paramType = "query")
    })
    @GetMapping("/inventoryAlarmList")
    public PageResult<InventoryAlarmVO> inventoryAlarmList(
            @RequestParam(defaultValue = "1") Integer page
            ,@RequestParam(defaultValue = "10") Integer limit
            ,@RequestParam(defaultValue = "create_time") String sort
            ,@RequestParam(defaultValue = "asc") String order
            ,Long demandId
    ){
        Page pg=new Page(page,limit);
        Map queryParam=new HashMap();
        queryParam.put("sort", StrUtil.toUnderlineCase(sort));
        queryParam.put("order",order);
        if(demandId != null){
            queryParam.put("demandId", demandId);
        }
        IPage<InventoryAlarmVO> list = inventoryAlarmService.pageList4app(pg,queryParam);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }

    @ApiOperation(value = "查询所有库存告警货道")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "第几页", defaultValue = "1", example = "1", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "limit", value = "每页多少条", defaultValue = "10", example = "10", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "sort", value="排序字段名", defaultValue = "create_time", example = "create_time", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "order", value="排序规则(asc:正序, desc:倒序)", defaultValue = "asc", example = "asc", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "alarmId", value="库存告警ID", defaultValue = "", example = "", dataType = "Long", paramType = "query")
    })
    @GetMapping("/inventoryAlarmDetailList")
    public PageResult<InventoryAlarmDetailVO> inventoryAlarmDetailList(
            @RequestParam(defaultValue = "1") Integer page
            ,@RequestParam(defaultValue = "10") Integer limit
            ,@RequestParam(defaultValue = "create_time") String sort
            ,@RequestParam(defaultValue = "asc") String order
            ,Long alarmId
    ){
        Page pg=new Page(page,limit);
        Map queryParam=new HashMap();
        queryParam.put("sort", StrUtil.toUnderlineCase(sort));
        queryParam.put("order",order);
        if(alarmId != null){
            queryParam.put("alarmId", alarmId);
        }
        IPage<InventoryAlarmDetailVO> list = inventoryAlarmDetailService.pageList4app(pg,queryParam);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }


    @ApiOperation(value = "获取包装条码的数据", notes = "搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", defaultValue = "1", example = "1", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "labelCode", value = "单号", defaultValue = "", example = "", dataType = "String", paramType = "query")
    })
    @GetMapping("/getPackLabel")
    public JsonResult<ProductPackLabelVO> getPackLabelVo(Long id, String labelCode){
        try{
            return cabinetLoadPlanService.getReceivePackLabel(id,labelCode);
        }catch (Exception e){
            log.error("get label fail",e);
            return JsonResult.fail(e.getMessage());
        }
    }

}
